package com.jade.linkong.service.impl;

import com.jade.common.constant.Constants;
import com.jade.common.core.text.Convert;
import com.jade.common.utils.DateUtils;
import com.jade.linkong.domain.LkProcessTaskFill;
import com.jade.linkong.domain.LkProject;
import com.jade.linkong.domain.LkProjectProcess;
import com.jade.linkong.mapper.LkProcessTaskFillMapper;
import com.jade.linkong.mapper.LkProjectMapper;
import com.jade.linkong.mapper.LkProjectProcessMapper;
import com.jade.linkong.service.ILkProjectProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 项目进度调度任务Service业务层处理
 * 
 * @author jade
 * @date 2020-08-06
 */
@Service
public class LkProjectProcessServiceImpl implements ILkProjectProcessService 
{
    @Autowired
    private LkProjectProcessMapper lkProjectProcessMapper;
    @Autowired
    private LkProjectMapper projectMapper;
    @Autowired
    private LkProcessTaskFillMapper fillMapper;

    public static final String SEGMENTATION = ",";
    /**
     * 查询项目进度调度任务
     * 
     * @param processId 项目进度调度任务ID
     * @return 项目进度调度任务
     */
    @Override
    public LkProjectProcess selectLkProjectProcessById(Long processId)
    {
        return lkProjectProcessMapper.selectLkProjectProcessById(processId);
    }

    /**
     * 查询项目进度调度任务列表
     * 
     * @param lkProjectProcess 项目进度调度任务
     * @return 项目进度调度任务
     */
    @Override
    public List<LkProjectProcess> selectLkProjectProcessList(LkProjectProcess lkProjectProcess)
    {
        return lkProjectProcessMapper.selectLkProjectProcessList(lkProjectProcess);
    }

    /**
     * 新增项目进度调度任务
     * 
     * @param lkProjectProcess 项目进度调度任务
     * @return 结果
     */
    @Override
    public int insertLkProjectProcess(LkProjectProcess lkProjectProcess)
    {
        lkProjectProcess.setCreateTime(DateUtils.getNowDate());
        return lkProjectProcessMapper.insertLkProjectProcess(lkProjectProcess);
    }

    /**
     * 修改项目进度调度任务
     * 
     * @param lkProjectProcess 项目进度调度任务
     * @return 结果
     */
    @Override
    public int updateLkProjectProcess(LkProjectProcess lkProjectProcess)
    {
        lkProjectProcess.setUpdateTime(DateUtils.getNowDate());
        return lkProjectProcessMapper.updateLkProjectProcess(lkProjectProcess);
    }

    /**
     * 删除项目进度调度任务对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkProjectProcessByIds(String ids)
    {
        return lkProjectProcessMapper.deleteLkProjectProcessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目进度调度任务信息
     * 
     * @param processId 项目进度调度任务ID
     * @return 结果
     */
    @Override
    public int deleteLkProjectProcessById(Long processId)
    {
        return lkProjectProcessMapper.deleteLkProjectProcessById(processId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int enableOrNotProcess(LkProjectProcess lkProjectProcess) {
        // 启用
         int row = -1;

        if(Constants.ENABLE == lkProjectProcess.getStatus()){
            //1.项目启用后插入填报表
            LkProjectProcess process = lkProjectProcessMapper.selectLkProjectProcessById(lkProjectProcess.getProcessId());
            List<LkProcessTaskFill> fills = new ArrayList<>();
            String[] split = null;
            // 判断周期是否是临时并且是否已下发过
            if("0".equals(process.getReportingCycle()) && process.getSendStatus() == Constants.UN_ENABLE){
                if(StringUtils.isNotBlank(process.getProjectIds())){
                     split = StringUtils.split(process.getProjectIds(), SEGMENTATION);
                }

                List<LkProject> lkProjects = projectMapper.selectProjectFileds(split);
                for(LkProject lkProject : lkProjects) {
                    if (lkProject.getStartTime() != null && new Date().getTime() > lkProject.getStartTime().getTime()) {
                        LkProcessTaskFill fill = new LkProcessTaskFill();
                        fill.setProcessId(process.getProcessId()).setProjectId(lkProject.getProId())
                                .setReportingCycle(process.getReportingCycle())
                                .setTaskName(process.getProcessName())
                                .setTaskType(process.getTaskType())
                                .setTaskNum(DateUtils.dateTimeNow())
                                .setProjectCode(lkProject.getProCode())
                                .setProjectName(lkProject.getProName())
                                .setProjectStartTime(lkProject.getStartTime())
                                .setTaskEndTime(lkProject.getEndTime())
                                .setCompanyName(lkProject.getCompanyName())
                                .setProjectLeaderPhone(lkProject.getLeaderPhone())
                                .setCreateTime(new Date());
//                        row = fillMapper.insertLkProcessTaskFill(fill);
                        fills.add(fill);
                    }
                }
                row = fillMapper.saveBatch(fills);
            }
        }
        lkProjectProcess.setSendStatus(0);
        row = lkProjectProcessMapper.updateLkProjectProcess(lkProjectProcess);
        return row;
    }
}
